

public class Sort {
    int []elem;
    public static void swap(int []array,int i,int j){
        int tmp=array[i];
        array[i]=array[j];
        array[j]=tmp;
    }
    public static void insetSort(int []array){
        for (int i = 1; i < array.length ; i++) {
            int tmp=array[i];
            int j=i-1;
            for (; j>=0;j--) {
                if(array[j+1]>tmp){
                    array[j+1]=array[j];
                }else {
                    array[j+1]=tmp;
                    break;
                }
            }
            array[j+1]=tmp;
        }
    }
    public static void shellSort(int []array){
        int gap= array.length;
        while(gap>1){
            gap/=2;
            shell(array,gap);
        }
    }
    public static  void shell(int []array,int gap){
        for (int i = gap; i < array.length ; i++) {
            int tmp=array[i];
            int j=i-gap;
            for (; j>=0;j-=gap) {
                if(array[j+gap]>tmp){
                    array[j+gap]=array[j];
                }else {
                    array[j+gap]=tmp;
                    break;
                }
            }
            array[j+gap]=tmp;
        }
    }
    public static void selectSort(int []array){
        for (int i = 0; i < array.length; i++) {
            int midIndex=i;
            for (int j =i+1; j < array.length; j++) {
                if(array[j]<array[i]){
                    midIndex=j;
                }
            }
            swap(array,midIndex,i);
        }
    }
    public static void selcetSort2(int []array){
        int left=0;
        int right= array.length-1;
        while(left<right){
            int minIndex=left;
            int maxIndex=left;
            for (int i = left+1; i <=right ; i++) {
                if(array[i]<array[minIndex]){
                    minIndex=i;
                }
                if(array[i]>array[maxIndex]){
                    maxIndex=i;
                }
                swap(array,left,minIndex);
                if(maxIndex==left){
                    maxIndex=minIndex;
                }
                swap(array,right,maxIndex);
                left++;
                right--;
            }
        }
    }
    public static void heapSort(int []array){
        createHeap(array);
        int end= array.length-1;
        while(end>0){
            swap(array,0,end);
            siftDowm(array,0,end);
            end--;
        }
    }
    public static void createHeap(int []array){
        for (int parent=(array.length-1-1)/2;parent>=0;parent--){
            siftDowm(array,parent,array.length);
        }
    }
    public static void siftDowm(int[]array,int parent,int len){
        int child=2*parent+1;
        while(child<len){
            if(child+1<len&&array[child]<array[child+1]){
                child++;
            }
            if(array[parent]<array[child]){
                swap(array,parent,child);
                parent=child;
                child=2*parent+1;
            }else {
                break;
            }
        }
    }
    public static void bubblesort(int []array){
        for (int i = 0; i < array.length-1; i++) {
            for (int j = 0; j < array.length-1-i; j++) {
                if(array[j]>array[j+1]){
                    swap(array,j,j+1);
                }
            }
        }
    }
    public static void bubblesort2(int []array){
        for (int i = 0; i < array.length-1; i++) {
            boolean flg=false;
            for (int j = 0; j < array.length-1-i; j++) {
                if(array[j]>array[j+1]){
                    swap(array,j,j+1);
                    flg=true;
                }
            }
            if(!flg){
                break;
            }
        }
    }
}
